package com.xiaojie.parse.model;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.*;

/**
 * 全局配置
 *
 * @author wangye
 * @classname Settings
 * @date 2021/6/25 14:03
 **/
@Data
@ToString
public class Settings implements Serializable {

    private static final long serialVersionUID = -3154819244426670956L;

    public static final Map<String, Merge> SETTING_ITEM_MERGE_MAP = Maps.newHashMap();

    static {
        SETTING_ITEM_MERGE_MAP.put("replaceFields", mergeReplaceFields());
        SETTING_ITEM_MERGE_MAP.put("autoGenerateFields", mergeAutoGenerateFields());
        SETTING_ITEM_MERGE_MAP.put("correspondDataFields", mergeCorrespondDataFields());
//        SETTING_ITEM_MERGE_MAP.put("queryByParams", mergeQueryByParams());
    }

    //查询条件{字段名:参数名,....}，多个查询条件and关系
    private Map<String, String> queryByParams;
    //替换字段{字段名:参数名,....}，根据参数替换表中字段数据
    private Map<String, String> replaceFields;
    //自动生成字段{字段名:实现类类全路径,....}
    private Map<String, String> autoGenerateFields;
    //获取表中需要保留旧数据与新数据对应的字段
    private List<String> correspondDataFields;
    //当前项目中查询数据时返回的map中的字段名如果为驼峰则配置true，否则false，默认为false
    private boolean fieldNameIsCamelCase;
    //排除通用配置中
    private List<String> excludeGlobalSettingItems;
    //没有任何查询条件情况下或条件值为空,查询表中所有数据
    private boolean queryAll = false;

    //导出生成压缩包内的文件名称,导入时根据此文件名超找对应的文件
    private String fileName;

    public boolean getFieldNameIsCamelCase() {
        return this.fieldNameIsCamelCase;
    }

    public void setFieldNameIsCamelCase(boolean fieldNameIsCamelCase) {
        this.fieldNameIsCamelCase = fieldNameIsCamelCase;
    }


    @FunctionalInterface
    public interface Merge<T> {

        T merge(T g, T t);

        static Map mergeMap(Map globalSettingItem, Map tableSettingItem) {
            Map map = new HashMap();
            if (MapUtil.isNotEmpty(globalSettingItem)) {
                map.putAll(globalSettingItem);
            }
            if (MapUtil.isNotEmpty(tableSettingItem)) {
                map.putAll(tableSettingItem);
            }
            return map;
        }

        static List mergeList(List globalSettingItem, List tableSettingItem) {
            Set set = new HashSet();
            if (CollectionUtil.isNotEmpty(globalSettingItem)) {
                set.addAll(globalSettingItem);
            }
            if (CollectionUtil.isNotEmpty(tableSettingItem)) {
                set.addAll(tableSettingItem);
            }
            return new ArrayList(set);
        }
    }

    private static Merge<Map> mergeReplaceFields() {
        return (g, t) -> Merge.mergeMap(g, t);
    }

    private static Merge<Map> mergeAutoGenerateFields() {
        return (g, t) -> Merge.mergeMap(g, t);
    }

    private static Merge<List> mergeCorrespondDataFields() {
        return (g, t) -> Merge.mergeList(g, t);
    }

    private static Merge<Map> mergeQueryByParams() {
        return (g, t) -> t != null ? t : g;
    }

}
